package com.program.beautymakeup.service;

import com.program.beautymakeup.common.Constraints;
import com.program.beautymakeup.domain.entity.Portfolio;
import com.program.beautymakeup.domain.entity.User;
import com.program.beautymakeup.domain.vo.PortfolioRsp;
import com.program.beautymakeup.mapper.PortfolioImgMapper;
import com.program.beautymakeup.mapper.PortfolioMapper;
import com.program.beautymakeup.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author bytedance
 * @ClassName HomeService.java
 * @Description TODO
 * @createTime 2023年02月09日 12:38:00
 */
@Service
public class HomeService {

    @Autowired
    private PortfolioMapper portfolioMapper;

    @Autowired
    private PortfolioImgMapper portfolioImgMapper;
    @Autowired
    private PortfolioService portfolioService;
    @Autowired
    private UserMapper userMapper;

    /**
     * 存储最近搜索的关键字
     */
    public static String[] recentSearchesKeyWord = new String[Constraints.maxRecentSearch];
    public static Integer idx = 0;

    public List<PortfolioRsp> getHomeList(Integer pageNum, Integer pageSize) {
        // 根据分页信息获得作品集
        List<Portfolio> portfolioList = portfolioMapper.selectAll(pageNum, pageSize, null);

        List<PortfolioRsp> portfolioRspList = portfolioService.findPortfolioRsp(portfolioList);

        return portfolioRspList;
    }

    public Map search(String keyword) {
        // 统计最近搜索的关键字
        addRecentSearchKeyWord(keyword);

        // 搜索化妆师
        List<User> users = userMapper.selectByUserName(keyword);

        // 搜索化妆集
        List<Portfolio> portfolioList = portfolioMapper.selectByName(keyword);
        List<PortfolioRsp> portfolioRspList = portfolioService.findPortfolioRsp(portfolioList);

        Map<String, Object> res = new HashMap();
        res.put("users", users);
        res.put("portfolioList", portfolioRspList);

        return res;
    }

    public List<PortfolioRsp> getAllList(Integer pageNum, Integer pageSize, Integer type) {
        List<Portfolio> portfolioList = portfolioMapper.selectAll(pageNum, pageSize, type);
        List<PortfolioRsp> portfolioRspList = portfolioService.findPortfolioRsp(portfolioList);
        return portfolioRspList;
    }

    public List<PortfolioRsp> getUserPortfoliosRsp(Integer userId, Integer type) {
        List<Portfolio> portfolioList = portfolioMapper.selectByUserIdAndType(userId, type);

        List<PortfolioRsp> portfolioRspList = portfolioService.findPortfolioRsp(portfolioList);

        return portfolioRspList;
    }


    public Set<String> recentSearch() {
        Set<String> ans = new HashSet<>();
        for (String s : recentSearchesKeyWord) {
            if (s != null) {
                ans.add(s);
            }
        }
        return ans;
    }

    public void addRecentSearchKeyWord(String keyword) {
        recentSearchesKeyWord[idx] = keyword;
        if (++idx >= Constraints.maxRecentSearch) {
            idx = 0;
        }
    }

}
